/*
 * Created on 22 ?.?. 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.softplus.carrefour.hd.frontend.servlet.process;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import java.math.BigDecimal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;

import org.apache.commons.fileupload.FileUploadException;

import com.epro.standard.frontend.datasetup.MessageManager;
import com.epro.standard.frontend.servlet.EproServlet;
import com.epro.standard.frontend.util.EproServletUtil;

import com.softplus.carrefour.CFException;
import com.softplus.carrefour.hd.CFHDSystem;
import com.softplus.carrefour.hd.bltier.admin.RoutePathManage;
import com.softplus.carrefour.hd.bltier.admin.UserLevelManage;
import com.softplus.carrefour.hd.bltier.db.HibernateUtil;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseDetail;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseDetailPK;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseDoc;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseDocPK;
import com.softplus.carrefour.hd.bltier.entity.CfhdCaseHead;
import com.softplus.carrefour.hd.bltier.entity.Country;
import com.softplus.carrefour.hd.bltier.entity.RoutePath;
import com.softplus.carrefour.hd.bltier.entity.RoutePathPK;
import com.softplus.carrefour.hd.bltier.entity.UserAccount;
import com.softplus.carrefour.hd.bltier.entity.UserLevel;
import com.softplus.carrefour.hd.bltier.pc.CCaseDetailManage;
import com.softplus.carrefour.hd.bltier.pc.CCaseDocManage;
import com.softplus.carrefour.hd.bltier.pc.CCaseHeadManage;
import com.softplus.carrefour.hd.bltier.pc.ICaseDetailControl;
import com.softplus.carrefour.hd.bltier.pc.ICaseDetailViewer;
import com.softplus.carrefour.hd.bltier.pc.ICaseDocControl;
import com.softplus.carrefour.hd.bltier.pc.ICaseDocViewer;
import com.softplus.carrefour.hd.bltier.pc.ICaseHeadControl;
import com.softplus.carrefour.hd.bltier.pc.ICaseHeadViewer;
import com.softplus.carrefour.hd.bltier.util.DateUtil;
import com.softplus.carrefour.hd.bltier.util.MailException;
import com.softplus.carrefour.hd.bltier.util.MailManager;
import com.softplus.carrefour.hd.bltier.util.httpUpload;

/**
 * @author Nancy
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class CFHDPC07 extends EproServlet {
    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {

        //		=================Select action for Managing==================>		
        this.modifyData(request, response);


        //=================Select action for Managing==================>		

    }

    //	-------------------------------------------------------------------------------------------------->
    //																										\\
    //													  Modify											\\
    //																										\\
    //	--------------------------------------------------------------------------------------------------->	

    public void modifyData(HttpServletRequest request, 
                           HttpServletResponse response) throws ServletException, 
                                                                IOException {
        System.out.println("^^^^^^^^servlet^^^modify^^^^^^^^^^^^");
        HttpSession session = request.getSession(true);
        EproServletUtil servletUtil = new EproServletUtil(request);

        //	======================Define redirect page==================>
        String parameterLink = "";
        String URL = "/CFHD/PC/CFHDPCUP07.jsp";
        String successLink = URL + "?check=true" + parameterLink;
        String unsuccessLink = URL + "?check=false" + parameterLink;
        Hashtable hash = new Hashtable();
        String CONTENT_TYPE = "text/html; charset=TIS-620";
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        httpUpload upload; //initial value
        try {
            ICaseDocControl docManage = 
                (CCaseDocManage)CFHDSystem.getControlClass("CASE_DOC");
            // ======= Define object for Doc ======== //
            upload = docManage.setCaseDoc(request);

            hash = upload.getFormFields();

            //	======================Define Variable======================>
            UserAccount userAcc = (UserAccount)session.getAttribute("userVO");
            Country country = userAcc.getCountry();


            //	======================Define Variable======================>

            String userID = 
                userAcc.getUserId() == null ? "" : userAcc.getUserId();
            String SEQDETAIL = (String)hash.get("SEQDETAIL");
            String CASEID = (String)hash.get("CASEID");
            String ORGACTION = (String)hash.get("ORGACTION");
            String ACTION = (String)hash.get("ACTION");
            String SYSTEM = (String)hash.get("SYSTEM");
            String MODULE = (String)hash.get("MODULE");
            String ALDATANO = (String)hash.get("ALDATANO");
            String GOLDVERSION = (String)hash.get("GOLDVERSION");
            String ENVIRONMENT = (String)hash.get("ENVIRONMENT");
            String SCREENNAME = (String)hash.get("SCREENNAME");
            String SELECTACTION = (String)hash.get("SELECTACTION");
            String CASETYPE = (String)hash.get("CASETYPE");
            String SEVERITY = (String)hash.get("SEVERITY");
            String REQUALIFYTO = (String)hash.get("REQUALIFYTO");
            Integer CHANGETO = 
                Integer.valueOf((String)hash.get("CHANGETO") == null ? "0" : 
                                (String)hash.get("CHANGETO"));
            String TRANSFERTO = (String)hash.get("TRANSFERTO");
            String TOUSERLEVEL = (String)hash.get("TOUSERLEVEL");
            String TYPEOFERROR = (String)hash.get("TYPEOFERROR");
            String DETAIL = (String)hash.get("DETAIL");
            String DESCRIPTION = (String)hash.get("DESCRIPTION");
            String UPDATEDATE = (String)hash.get("UPDATEDATE");
            String OLD_FILE = (String)hash.get("OLD_FILE");
            String rptBy = 
                (String)hash.get("RPTBY") == null ? "" : (String)hash.get("RPTBY");
            String DWNumber = 
                (String)hash.get("DWNUMBER") == null ? "" : (String)hash.get("DWNUMBER");
            String sendEMail = "";

            successLink = successLink + "&caseID=" + CASEID;
            unsuccessLink = unsuccessLink + "&caseID=" + CASEID;
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            System.out.println("upload =============" + upload.getFileName() + 
                               "Begin");
            int fileSize = 0;
            if (!upload.getFileName().equals("")) {
                fileSize = upload.getFileSize();
                System.out.println(" =======fileSize======" + fileSize);
            }
            if (fileSize < upload.getMaxFileSize()) {
                /*****************/
                //	======================Define Object======================>
                /*************************************************************/
                ICaseHeadControl headManage = 
                    (CCaseHeadManage)CFHDSystem.getControlClass("CASE_HEAD");
                ICaseHeadViewer headView = 
                    (CCaseHeadManage)CFHDSystem.getControlClass("CASE_HEAD");
                CfhdCaseHead headEntity = new CfhdCaseHead();
                //						headEntity = headView.getCaseHeadDesc(CASEID);
                //						CfhdCaseHead	 		  headEntity2		= new CfhdCaseHead();
                System.out.println("07=====session headEntity =====" + 
                                   (CfhdCaseHead)session.getAttribute("headEntity"));

                headEntity = (CfhdCaseHead)session.getAttribute("headEntity");

                if (headEntity == null) {
                    headEntity = headView.getCaseHeadDesc(CASEID);
                    System.out.println("yoyoyo");
                    //Hibernate.initialize(headEntity);
                }

                //		headEntity.setAlDataNo(headEntity2.getAlDataNo());
                //		headEntity.setAutoEscalateStatus(headEntity2.isAutoEscalateStatus());
                //		headEntity.setCaseId(headEntity2.getCaseId());
                //		headEntity.setCaseStatus(headEntity2.getCaseStatus());
                //		headEntity.setCaseType(headEntity2.getCaseType());
                //		headEntity.setCfhdCaseDetails(headEntity2.getCfhdCaseDetails());
                //		headEntity.setCfhdCaseKeywords(headEntity2.getCfhdCaseKeywords());
                //		headEntity.setCfhdEnvironment(headEntity2.getCfhdEnvironment());
                //		headEntity.setCfhdModule(headEntity2.getCfhdModule());
                //		headEntity.setCfhdSystem(headEntity2.getCfhdSystem());
                //		headEntity.setCloseCaseDate(headEntity2.getCloseCaseDate());
                //		headEntity.setCountry(headEntity2.getCountry());
                //		headEntity.setCountryId(headEntity2.getCountryId());
                //		headEntity.setCredBy(headEntity2.getCredBy());
                //		headEntity.setCredDate(headEntity2.getCredDate());
                //		headEntity.setDuration(headEntity2.getDuration());
                //		headEntity.setGoldVersion(headEntity2.getGoldVersion());
                //		headEntity.setMdStore(headEntity2.getMdStore());
                //		headEntity.setOpenCaseDate(headEntity2.getOpenCaseDate());
                //		headEntity.setOpenStatus(headEntity2.isOpenStatus());
                //		headEntity.setOriginCaseType(headEntity2.getOriginCaseType());
                //		headEntity.setOriginSeverity(headEntity2.getOriginSeverity());
                //		headEntity.setReqFax(headEntity2.getReqFax());
                //		headEntity.setReqName(headEntity2.getReqName());
                //		headEntity.setReqTel(headEntity2.getReqTel());
                //		headEntity.setRouteDirection(headEntity2.getRouteDirection());
                //		headEntity.setScreenName(headEntity2.getScreenName() );
                //		headEntity.setSeverity(headEntity2.getSeverity());
                //		headEntity.setStoreId(headEntity2.getStoreId());
                //		headEntity.setSubject(headEntity2.getSubject());
                //		headEntity.setUpdBy(headEntity2.getUpdBy());
                //		headEntity.setUpdDate(headEntity2.getUpdDate());
                //		headEntity.setUserAccount(headEntity2.getUserAccount());
                //		headEntity.setUserId(headEntity2.getUserId());

                String oldRequalify = headEntity.getCaseType();
                Integer oldSeverity = headEntity.getSeverity();


                /*************************************************************/
                ICaseDetailControl detailManage = 
                    (CCaseDetailManage)CFHDSystem.getControlClass("CASE_DETAIL");
                ICaseDetailViewer detailViwer = 
                    (CCaseDetailManage)CFHDSystem.getControlClass("CASE_DETAIL");
                CfhdCaseDetail detailEntity = new CfhdCaseDetail();
                detailEntity = 
                        detailViwer.getCaseDetailDesc(new CfhdCaseDetailPK(CASEID, 
                                                                           new BigDecimal(SEQDETAIL)));
                UserLevelManage userManage = new UserLevelManage();
                String userLevel = userManage.getUserLevelByUserID(userID);

                System.out.println("======+++++++++Begin Set HeadCase+++++++++======");
                headEntity.setCfhdSystem(SYSTEM);
                headEntity.setCfhdModule(MODULE);

                headEntity.setAlDataNo(ALDATANO);
                headEntity.setGoldVersion(GOLDVERSION);
                headEntity.setCfhdEnvironment(ENVIRONMENT);
                headEntity.setScreenName(SCREENNAME);
                headEntity.setUpdBy(userID);
                headEntity.setReportedBy(rptBy);
                headEntity.setDwNumber(DWNumber);

                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                System.out.println("======+++++++++Begin Set DetailCase+++++++++======");
                if (SELECTACTION.equals(CfhdCaseDetail.ACTION_ANSWER)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType(TYPEOFERROR);
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom("");
                    detailEntity.setTransferTo("");
                    detailEntity.setTransferToLv(TOUSERLEVEL);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();


                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_ANSWER=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";
                    } else {
                        System.out.println("=========submit======ACTION_ANSWER=========");
                        sendEMail = "SUBMIT_ANSWER";
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        /////////////////////////////////////
                        String userl = 
                            new UserLevelManage().getUserLevel(userID).getUlevel();
                        RoutePathManage routePath = new RoutePathManage();
                        RoutePath routeP = 
                            routePath.getRoutePathByPK(new RoutePathPK(oldRequalify, 
                                                                       userl, 
                                                                       TOUSERLEVEL));
                        if (routeP != null) {

                            headEntity.setRouteDirection(routeP.getRouteDirection());
                            if (routeP.getRouteDirection().equals("R")) {
                                System.out.println("==RouteDirection()=in=ACTION_ANSWER==RR");
                                headEntity.setAutoEscalateStatus(false);
                            }
                            headEntity.setAutoEscalateStatus(false);
                        } else {
                            //????????? user level 1
                            headEntity.setRouteDirection("R");
                            headEntity.setAutoEscalateStatus(false);
                            System.out.println("null==RouteDirection()=in=ACTION_ANSWER==R");
                        }

                        if (rptBy.equals("")) {
                            if (userl.equals(UserLevel.LEVEL_1_COUNTRY_HELPDESK)) {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_FINISH);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_FINISH);
                                headEntity.setOpenStatus(false);
                            } else {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                                headEntity.setOpenStatus(true);
                            }
                        } else {
                            System.out.println("#########2222###########" + 
                                               userl);
                            if (userl.equals(UserLevel.LEVEL_2_AMS_ZONE_ASIA)) {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_FINISH);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_FINISH);
                                headEntity.setOpenStatus(false);
                            } else {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                                headEntity.setOpenStatus(true);
                            }
                        }

                        System.out.println("=================begin==============");
                        successLink = successLink + "&submitSuccess=true";
                        System.out.println("=================end==============");
                    }


                    System.out.println("1Begin===================Modify Head Complete ====================");
                    HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                    HibernateUtil.getSession().evict(detailEntity);
                    headManage.updateCfhdCaseHead(headEntity);
                    System.out.println("1Strat===================Modify Head Complete ====================");

                    System.out.println("1=================== Detail Begin Insert===================");
                    detailManage.updateCfhdCaseDetail(detailEntity);
                    System.out.println("1=================== Detail Complete ====================");

                    System.out.println("1**********************************End case detail********************************");

                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_MANUAL_ESCALATE)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom("");
                    detailEntity.setTransferTo("");
                    detailEntity.setTransferToLv(TOUSERLEVEL);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    //	String userl = new UserLevelManage().getUserLevel(userID).getUlevel();
                    //	RoutePathManage routePath = new RoutePathManage();
                    //	RoutePath routeP =   routePath.getRoutePathByPK(new RoutePathPK(CASEID,userl,TOUSERLEVEL));
                    //	if(routeP!=null)
                    //	{
                    //	headEntity.setRouteDirection(routeP.getRouteDirection());
                    //	}

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_MANUAL_ESCALATE=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";
                    } else {
                        System.out.println("=========submit======ACTION_MANUAL_ESCALATE=========");
                        sendEMail = "SUBMIT_MANUAL_ESCALATE";
                        headEntity.setOpenStatus(true);
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        /////////////////////////////////////
                        String userl = 
                            new UserLevelManage().getUserLevel(userID).getUlevel();
                        RoutePathManage routePath = new RoutePathManage();
                        RoutePath routeP = 
                            routePath.getRoutePathByPK(new RoutePathPK(oldRequalify, 
                                                                       userl, 
                                                                       TOUSERLEVEL));
                        System.out.println("R=========oldRequalify==============" + 
                                           oldRequalify);
                        System.out.println("R=========userl==============" + 
                                           userl);
                        System.out.println("R=========TOUSERLEVEL==============" + 
                                           TOUSERLEVEL);
                        if (routeP != null) {
                            headEntity.setRouteDirection(routeP.getRouteDirection());
                        } else {
                            //????????? user level 1
                            System.out.println("R=========else==============");
                            headEntity.setRouteDirection("R");
                        }

                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);

                        System.out.println("=================begin==============");
                        successLink = successLink + "&submitSuccess=true";
                        System.out.println("=================end==============");
                    }


                    System.out.println("22Begin===================Modify Head Complete ====================");
                    HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                    HibernateUtil.getSession().evict(detailEntity);
                    headManage.updateCfhdCaseHead(headEntity);
                    System.out.println("2Strat===================Modify Head Complete ====================");

                    System.out.println("2=================== Detail Begin Insert===================");
                    detailManage.updateCfhdCaseDetail(detailEntity);
                    System.out.println("2=================== Detail Complete ====================");

                    //							System.out.println("2+++++Begin Send to Mail Escalate++++++");		
                    //							MailManager mm  = new  MailManager();
                    //							mm.mailEscalateCase(userID,CASEID);
                    //							System.out.println("2+++++End Send to Mail Escalate++++++");		

                    System.out.println("2**********************************End case detail********************************");

                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_REJECT)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom("");
                    detailEntity.setTransferTo("");
                    detailEntity.setTransferToLv(TOUSERLEVEL);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_REJECT=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";
                    } else {
                        System.out.println("=========submit======ACTION_REJECT=========");
                        sendEMail = "SUBMIT_REJECT";
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        /////////////////////////////////////
                        String userl = 
                            new UserLevelManage().getUserLevel(userID).getUlevel();
                        RoutePathManage routePath = new RoutePathManage();
                        RoutePath routeP = 
                            routePath.getRoutePathByPK(new RoutePathPK(oldRequalify, 
                                                                       userl, 
                                                                       TOUSERLEVEL));
                        if (routeP != null) {
                            headEntity.setRouteDirection(routeP.getRouteDirection());
                            if (routeP.getRouteDirection().equals("R")) {
                                System.out.println("==RouteDirection()=in=ACTION_REJECT==RR");
                                headEntity.setAutoEscalateStatus(false);
                            }
                        } else {
                            //????????? user level 1
                            System.out.println("null==RouteDirection()=in=ACTION_REJECT==R");
                            headEntity.setAutoEscalateStatus(false);
                            headEntity.setRouteDirection("R");
                        }

                        if (rptBy.equals("")) {
                            if (userl.equals(UserLevel.LEVEL_1_COUNTRY_HELPDESK)) {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_CANCEL);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_CANCEL);
                                headEntity.setOpenStatus(false);
                            } else {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_REJECTED);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_REJECTED);
                                headEntity.setOpenStatus(true);
                            }
                        } else {
                            if (userl.equals(UserLevel.LEVEL_2_AMS_ZONE_ASIA)) {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_CANCEL);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_CANCEL);
                                headEntity.setOpenStatus(false);
                            } else {
                                headEntity.setCaseStatus(CfhdCaseHead.STATUS_REJECTED);
                                detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_REJECTED);
                                headEntity.setOpenStatus(true);
                            }
                        }


                        System.out.println("=================begin==============");
                        successLink = successLink + "&submitSuccess=true";
                        System.out.println("=================end==============");
                    }


                    System.out.println("3Begin===================Modify Head Complete ====================");
                    HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                    HibernateUtil.getSession().evict(detailEntity);
                    headManage.updateCfhdCaseHead(headEntity);
                    System.out.println("3Strat===================Modify Head Complete ====================");

                    System.out.println("3=================== Detail Begin Insert===================");
                    detailManage.updateCfhdCaseDetail(detailEntity);
                    System.out.println("3=================== Detail Complete ====================");

                    System.out.println("3**********************************End case detail********************************");

                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_TRANSFER)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom(userID);
                    detailEntity.setTransferTo(TRANSFERTO);
                    detailEntity.setTransferToLv(userLevel);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_TRANSFER=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";

                        System.out.println("4.1Begin===================Modify Head Complete ====================");
                        HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                        HibernateUtil.getSession().evict(detailEntity);
                        headManage.updateCfhdCaseHead(headEntity);
                        System.out.println("4.1Strat===================Modify Head Complete ====================");

                        System.out.println("4.1=================== Detail Begin Insert===================");
                        detailManage.updateCfhdCaseDetail(detailEntity);
                        System.out.println("4.1=================== Detail Complete ====================");

                        System.out.println("**********************************End case detail********************************");
                    } else {
                        System.out.println("=========submit======ACTION_TRANSFER=========");
                        sendEMail = "SUBMIT_TRANSFER";
                        headEntity.setOpenStatus(true);
                        //headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        System.out.println("=================begin==============");
                        //		if(!SELECTACTION.equals(CfhdCaseDetail.ACTION_TRANSFER))
                        //		{ 			
                        //			successLink = successLink+"&submitSuccess=true";
                        //			//successLink = successLink+"&submitSuccess=true&caseno="+CASEID;//"../PC/CFHDPCMT06.jsp?caseID="+CASEID;
                        //		}else{
                        //								if(ACTION.equals("submit"))
                        //									{
                        CfhdCaseDetail detailEntity2 = new CfhdCaseDetail();
                        int seq = new Integer(SEQDETAIL).intValue() + 1;
                        detailEntity2.setComp_id(new CfhdCaseDetailPK(CASEID, 
                                                                      new BigDecimal(seq)));
                        detailEntity2.setOpenedTime(com.softplus.carrefour.hd.bltier.util.DateUtil.getLocalDate(userID));
                        detailEntity2.setUserId(TRANSFERTO);
                        detailEntity2.setCountryId(country.getCountryId());
                        //detailEntity.setCaseDetStatus(headEntity.getCaseStatus());

                        detailEntity2.setCredBy(userID);
                        //detailEntity.setCredDate(DateUtil.getLocalDate(userID));
                        detailEntity2.setUpdBy(userID);
                        //detailEntity.setUpdDate(DateUtil.getLocalDate(userID));

                        //detailEntity2.setCaseAction(SELECTACTION);
                        detailEntity2.setCaseDetType(oldRequalify);
                        if (oldRequalify.equals(CfhdCaseHead.CASE_SERVICE_REQUEST)) {
                            detailEntity2.setDateToApply(detailEntity.getDateToApply());
                        }
                        detailEntity2.setCaseDetServerity(oldSeverity);

                        //detailEntity2.setDetDesc(DETAIL);
                        detailEntity2.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity2.setOpenedTime(DateUtil.getLocalDate(userID));
                        headEntity.setUserId(TRANSFERTO);

                        //headEntity.setOpenStatus(true);
                        //detailEntity2.setTransferTo(TRANSFERTO);
                        //detailEntity2.setTransferFrom(userID);
                        //detailEntity2.setTransferToLv(userLevel);		

                        detailEntity2.setTransferToLv(userLevel);
                        //	successLink = successLink+"&submitSuccess=false";				


                        System.out.println("4.2Begin===================Modify Head Complete ====================");
                        HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                        HibernateUtil.getSession().evict(detailEntity);
                        headManage.updateCfhdCaseHead(headEntity);
                        System.out.println("4.2Strat===================Modify Head Complete ====================");

                        System.out.println("4.2=================== Detail Begin Insert===================");
                        detailManage.updateCfhdCaseDetail(detailEntity);
                        System.out.println("=================== Detail Complete ====================");

                        System.out.println("4.2**********************************End case detail********************************");
                        System.out.println("4.2================= Prepare Detail Value  Insert2222222=======================" + 
                                           seq);
                        detailManage.insertCfhdCaseDetail(detailEntity2);
                        System.out.println("4.2=================end Detail Value  Insert2222222222=======================");
                        successLink = successLink + "&submitSuccess=true";

                        //																								System.out.println("4.2+++++Begin Send to Mail Transfer++++++");		
                        //																								MailManager mm  = new  MailManager();
                        //																								mm.mailTransferCase(CASEID,userID,TRANSFERTO);
                        //																								System.out.println("4.2+++++End Send to Mail Transfer++++++");		
                        //			}
                        //}																			
                        System.out.println("=================end==============");
                    }
                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_REQUALIFY)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(REQUALIFYTO);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom(userID);
                    detailEntity.setTransferTo(userID);
                    detailEntity.setTransferToLv(userLevel);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_REQUALIFY=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";


                        System.out.println("5.1Begin===================Modify Head Complete ====================");
                        HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                        HibernateUtil.getSession().evict(detailEntity);
                        headManage.updateCfhdCaseHead(headEntity);
                        System.out.println("5.1Strat===================Modify Head Complete ====================");

                        System.out.println("5.1=================== Detail Begin Insert===================");
                        detailManage.updateCfhdCaseDetail(detailEntity);
                        System.out.println("5.1=================== Detail Complete ====================");

                        System.out.println("5.1**********************************End case detail********************************");
                    } else {
                        if ((REQUALIFYTO.equals(CfhdCaseHead.CASE_CHANGE_REQUEST)) && 
                            (userLevel.equals(UserLevel.LEVEL_2_AMS_ZONE_ASIA) || 
                             userLevel.equals(UserLevel.LEVEL_3_AMS_BANGKOK) || 
                             userLevel.equals(UserLevel.PRODUCTION_STAFF))) {
                            System.out.println("***********CASE_CHANGE_REQUEST=========submit======ACTION_REQUALIFY====");
                            headEntity.setOpenStatus(true);
                            headEntity.setCaseType(REQUALIFYTO);
                            headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                            headEntity.setRouteDirection("R");
                            detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                            detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                            detailEntity.setTransferFrom("");
                            detailEntity.setTransferTo("");
                            detailEntity.setTransferToLv(UserLevel.LEVEL_2_COUNTRY_APPLICATION_TEAM_BACK_OFFICE);

                            System.out.println("5.2Begin===================Modify Head Complete ====================");
                            HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                            HibernateUtil.getSession().evict(detailEntity);
                            headManage.updateCfhdCaseHead(headEntity);
                            System.out.println("5.2Strat===================Modify Head Complete ====================");

                            System.out.println("5.2=================== Detail Begin Insert===================");
                            detailManage.updateCfhdCaseDetail(detailEntity);
                            System.out.println("5.2=================== Detail Complete ====================");

                            successLink = successLink + "&submitSuccess=true";

                            //			}
                            //}

                            System.out.println("=================end========ACTION_REQUALIFY======");

                        } else {

                            System.out.println("=========submit======ACTION_REQUALIFY=========");
                            //headEntity.setOpenStatus(true);
                            headEntity.setCaseType(REQUALIFYTO);
                            //headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                            detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                            detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                            System.out.println("=================begin=====ACTION_REQUALIFY=========");
                            //		if(!SELECTACTION.equals(CfhdCaseDetail.ACTION_TRANSFER))
                            //		{ 			
                            //			successLink = successLink+"&submitSuccess=true";
                            //			//successLink = successLink+"&submitSuccess=true&caseno="+CASEID;//"../PC/CFHDPCMT06.jsp?caseID="+CASEID;
                            //		}else{
                            //								if(ACTION.equals("submit"))
                            //									{
                            CfhdCaseDetail detailEntity2 = 
                                new CfhdCaseDetail();
                            int seq = new Integer(SEQDETAIL).intValue() + 1;
                            detailEntity2.setComp_id(new CfhdCaseDetailPK(CASEID, 
                                                                          new BigDecimal(seq)));
                            detailEntity2.setOpenedTime(com.softplus.carrefour.hd.bltier.util.DateUtil.getLocalDate(userID));
                            detailEntity2.setUserId(userID);
                            detailEntity2.setCountryId(country.getCountryId());
                            //detailEntity.setCaseDetStatus(headEntity.getCaseStatus());

                            detailEntity2.setCredBy(userID);
                            //detailEntity.setCredDate(DateUtil.getLocalDate(userID));
                            detailEntity2.setUpdBy(userID);
                            //detailEntity.setUpdDate(DateUtil.getLocalDate(userID));

                            //detailEntity2.setCaseAction(SELECTACTION);
                            detailEntity2.setCaseDetType(REQUALIFYTO);
                            if (REQUALIFYTO.equals(CfhdCaseHead.CASE_SERVICE_REQUEST)) {
                                detailEntity2.setDateToApply(detailEntity.getDateToApply());
                            }
                            detailEntity2.setCaseDetServerity(oldSeverity);

                            //detailEntity2.setDetDesc(DETAIL);
                            detailEntity2.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                            headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                            detailEntity2.setOpenedTime(DateUtil.getLocalDate(userID));
                            headEntity.setUserId(userID);
                            //headEntity.setOpenStatus(true);
                            //detailEntity2.setTransferTo(TRANSFERTO);
                            //detailEntity2.setTransferFrom(userID);
                            //detailEntity2.setTransferToLv(userLevel);		

                            //	detailEntity2.setTransferToLv(userLevel);
                            //	successLink = successLink+"&submitSuccess=false";									


                            System.out.println("5.2Begin===================Modify Head Complete ====================");
                            HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                            HibernateUtil.getSession().evict(detailEntity);
                            headManage.updateCfhdCaseHead(headEntity);
                            System.out.println("5.2Strat===================Modify Head Complete ====================");

                            System.out.println("5.2=================== Detail Begin Insert===================");
                            detailManage.updateCfhdCaseDetail(detailEntity);
                            System.out.println("5.2=================== Detail Complete ====================");

                            System.out.println("5.2**********************************End case detail********************************");
                            System.out.println("5.2================= Prepare Detail Value  Insert2222222=======================" + 
                                               seq);
                            detailManage.insertCfhdCaseDetail(detailEntity2);
                            System.out.println("5.2=================end Detail Value  Insert2222222222=======================");
                            successLink = successLink + "&submitSuccess=false";

                            //			}
                            //}

                            System.out.println("=================end========ACTION_REQUALIFY======");

                        }


                    }


                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_CHANGE_SERVERITY)) {

                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(CHANGETO);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom(userID);
                    detailEntity.setTransferTo(userID);
                    detailEntity.setTransferToLv(userLevel);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_CHANGE_SERVERITY=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";


                        System.out.println("6.1Begin===================Modify Head Complete ====================");
                        HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                        HibernateUtil.getSession().evict(detailEntity);
                        headManage.updateCfhdCaseHead(headEntity);
                        System.out.println("6.1Strat===================Modify Head Complete ====================");

                        System.out.println("6.1=================== Detail Begin Insert===================");
                        detailManage.updateCfhdCaseDetail(detailEntity);
                        System.out.println("6.1=================== Detail Complete ====================");

                        System.out.println("6.1**********************************End case detail********************************");
                    } else {
                        System.out.println("=========submit======ACTION_CHANGE_SERVERITY=========");
                        //headEntity.setOpenStatus(true);
                        headEntity.setSeverity(CHANGETO);
                        //headEntity.setCaseStatus(CfhdCaseHead.STATUS_SUBMITTED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_SUBMITTED);
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        System.out.println("=================begin=====ACTION_CHANGE_SERVERITY=========");
                        //		if(!SELECTACTION.equals(CfhdCaseDetail.ACTION_TRANSFER))
                        //		{ 			
                        //			successLink = successLink+"&submitSuccess=true";
                        //			//successLink = successLink+"&submitSuccess=true&caseno="+CASEID;//"../PC/CFHDPCMT06.jsp?caseID="+CASEID;
                        //		}else{
                        //								if(ACTION.equals("submit"))
                        //									{
                        CfhdCaseDetail detailEntity2 = new CfhdCaseDetail();
                        int seq = new Integer(SEQDETAIL).intValue() + 1;
                        detailEntity2.setComp_id(new CfhdCaseDetailPK(CASEID, 
                                                                      new BigDecimal(seq)));
                        detailEntity2.setOpenedTime(com.softplus.carrefour.hd.bltier.util.DateUtil.getLocalDate(userID));
                        detailEntity2.setUserId(userID);
                        detailEntity2.setCountryId(country.getCountryId());
                        //detailEntity.setCaseDetStatus(headEntity.getCaseStatus());

                        detailEntity2.setCredBy(userID);
                        //detailEntity.setCredDate(DateUtil.getLocalDate(userID));
                        detailEntity2.setUpdBy(userID);
                        //detailEntity.setUpdDate(DateUtil.getLocalDate(userID));

                        //detailEntity2.setCaseAction(SELECTACTION);
                        detailEntity2.setCaseDetType(oldRequalify);
                        if (oldRequalify.equals(CfhdCaseHead.CASE_SERVICE_REQUEST)) {
                            detailEntity2.setDateToApply(detailEntity.getDateToApply());
                        }
                        detailEntity2.setCaseDetServerity(CHANGETO);

                        //detailEntity2.setDetDesc(DETAIL);
                        detailEntity2.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity2.setOpenedTime(DateUtil.getLocalDate(userID));
                        headEntity.setUserId(userID);
                        //headEntity.setOpenStatus(true);
                        //detailEntity2.setTransferTo(TRANSFERTO);
                        //detailEntity2.setTransferFrom(userID);
                        //detailEntity2.setTransferToLv(userLevel);		

                        //	detailEntity2.setTransferToLv(userLevel);
                        //	successLink = successLink+"&submitSuccess=false";									


                        System.out.println("6.2Begin===================Modify Head Complete ====================");
                        HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                        HibernateUtil.getSession().evict(detailEntity);
                        headManage.updateCfhdCaseHead(headEntity);
                        System.out.println("6.2Strat===================Modify Head Complete ====================");

                        System.out.println("6.2=================== Detail Begin Insert===================");
                        detailManage.updateCfhdCaseDetail(detailEntity);
                        System.out.println("6.2=================== Detail Complete ====================");

                        System.out.println("6.2**********************************End case detail********************************");
                        System.out.println("6.2================= Prepare Detail Value  Insert2222222=======================" + 
                                           seq);
                        detailManage.insertCfhdCaseDetail(detailEntity2);
                        System.out.println("6.2=================end Detail Value  Insert2222222222=======================");
                        successLink = successLink + "&submitSuccess=false";

                        //			}
                        //}

                        System.out.println("=================end========ACTION_REQUALIFY======");
                    }

                } else if (SELECTACTION.equals(CfhdCaseDetail.ACTION_APPROVE)) {
                    detailEntity.setCaseAction(SELECTACTION);
                    detailEntity.setCaseDetServerity(oldSeverity);
                    /******detailEntity.setCaseDetStatus();***/
                    detailEntity.setCaseDetType(oldRequalify);
                    //detailEntity.setCfhdCaseDoc();
                    //detailEntity.setCfhdCaseHead();
                    detailEntity.setCfhdErrType("");
                    //detailEntity.setComp_id();
                    //detailEntity.setCountry();
                    //detailEntity.setCountryId();
                    //detailEntity.setCredBy();
                    //detailEntity.setCredDate();
                    detailEntity.setDetDesc(DETAIL);
                    //detailEntity.setDetType();
                    //detailEntity.setOpenedTime();
                    //detailEntity.setSubmitTime();
                    detailEntity.setTransferFrom("");
                    detailEntity.setTransferTo("");
                    detailEntity.setTransferToLv(UserLevel.LEVEL_2_COUNTRY_APPLICATION_TEAM_BACK_OFFICE);
                    detailEntity.setUpdBy(userID);
                    //detailEntity.setUpdDate();
                    //detailEntity.setUserAccount();
                    //detailEntity.setUserId();

                    //	String userl = new UserLevelManage().getUserLevel(userID).getUlevel();
                    //	RoutePathManage routePath = new RoutePathManage();
                    //	RoutePath routeP =   routePath.getRoutePathByPK(new RoutePathPK(CASEID,userl,TOUSERLEVEL));
                    //	if(routeP!=null)
                    //	{
                    //	headEntity.setRouteDirection(routeP.getRouteDirection());
                    //	}

                    if (ACTION.equals("save")) {
                        System.out.println("=========save======ACTION_APPROVE=========");
                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_VALIDATED);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_VALIDATED);
                        successLink = successLink + "&submitSuccess=false";
                    } else {
                        System.out.println("=========submit======ACTION_APPROVE=========");
                        headEntity.setOpenStatus(true);
                        detailEntity.setSubmitTime(DateUtil.getLocalDate(userID));

                        /////////////////////////////////////
                        //															String userl = new UserLevelManage().getUserLevel(userID).getUlevel();
                        //															RoutePathManage routePath = new RoutePathManage();
                        //															RoutePath routeP =   routePath.getRoutePathByPK(new RoutePathPK(oldRequalify,userl,TOUSERLEVEL));
                        //															System.out.println("R=========oldRequalify=============="+oldRequalify);	
                        //															System.out.println("R=========userl=============="+userl);	
                        //															System.out.println("R=========TOUSERLEVEL=============="+TOUSERLEVEL);	
                        //															if(routeP!=null)
                        //															{
                        //															headEntity.setRouteDirection(routeP.getRouteDirection());
                        //															}else{
                        //????????? user level 1
                        System.out.println("========R===========");
                        headEntity.setRouteDirection("R");
                        //															}

                        headEntity.setCaseStatus(CfhdCaseHead.STATUS_APPROVE);
                        detailEntity.setCaseDetStatus(CfhdCaseHead.STATUS_APPROVE);

                        System.out.println("=================begin==============");
                        successLink = successLink + "&submitSuccess=true";
                        System.out.println("=================end==============");
                    }


                    System.out.println("7Begin===================Modify Head Complete ====================");
                    HibernateUtil.getSession().evict(detailEntity.getCfhdCaseHead());
                    HibernateUtil.getSession().evict(detailEntity);
                    headManage.updateCfhdCaseHead(headEntity);
                    System.out.println("7Strat===================Modify Head Complete ====================");

                    System.out.println("7=================== Detail Begin Insert===================");
                    detailManage.updateCfhdCaseDetail(detailEntity);
                    System.out.println("7=================== Detail Complete ====================");

                    System.out.println("7**********************************End case detail********************************");

                }
                if (headEntity.getStoreId() != null) {
                    Hibernate.initialize(headEntity.getMdStore());
                }
                if (headEntity.getCountryId() != null) {
                    Hibernate.initialize(headEntity.getCountry());
                }
                session.setAttribute("headEntity", headEntity);
                if (!upload.getFileName().equals("")) {
                    System.out.println("upload =============");
                    ICaseDocViewer caseDoc = 
                        (CCaseDocManage)CFHDSystem.getControlClass("CASE_DOC");
                    //		if(caseDoc.getCaseDocByPK(new CfhdCaseDocPK(CASEID,detailEntity.getComp_id().getCaseDetSeq(),new BigDecimal(0)))!=null)		
                    //			docManage.deleteCaseDoc(new CfhdCaseDoc(new CfhdCaseDocPK(CASEID,detailEntity.getComp_id().getCaseDetSeq(),new BigDecimal(0))));
                    CfhdCaseDoc caseDocEntity = 
                        caseDoc.getCaseDocByPK(new CfhdCaseDocPK(CASEID, 
                                                                 new BigDecimal(SEQDETAIL), 
                                                                 new BigDecimal(0)));

                    System.out.println(" document exist? " + caseDocEntity);

                    if (caseDocEntity != null) {
                        HibernateUtil.getSession().clear();
                        docManage.deleteCaseDoc(new CfhdCaseDoc(new CfhdCaseDocPK(CASEID, 
                                                                                  new BigDecimal(SEQDETAIL), 
                                                                                  new BigDecimal(0))));
                    }


                    try {
                        //upload = new httpUpload(request);

                        // Set Table Name
                        upload.setTableName("CFHD_CASE_DOC");

                        // Add primary key
                        upload.addColumnPK("CASE_ID");
                        upload.addColumnPK("CASE_DET_SEQ");
                        upload.addColumnPK("CASE_DOC_SEQ");
                        upload.addColumnPK("ATT_DESC");

                        // Set file name column
                        upload.setColumnFileName("ATT_FILE_NAME");

                        // Add Value to PK
                        //Hashtable hash = upload.getFormFields();
                        upload.addValuePK("'" + CASEID + "'"); // Varchar
                        upload.addValuePK(detailEntity.getComp_id().getCaseDetSeq().toString()); // Number
                        upload.addValuePK("0"); // Number
                        upload.addValuePK("'" + DESCRIPTION + "'"); // Varchar

                        // Set LOB Column
                        upload.setLOBColumnName("ATT_FILE");

                        // Set file is BLOB or CLOB
                        upload.setBLOB();
                        //upload.setDataUnique("'"+caseId+"',"+detSeq+","+docSeq);

                        // Set Destination is DATABASE or FILEBASE
                        upload.setDestination("DATABASE");
                        // (servletUtil.getRequestValue("action")).toLowerCase() ; 


                        // Set Connection use package com.epro.standard.infrastructure.dataaccess.DBManage
                        Connection conn = 
                            (Connection)HibernateUtil.getConnection();
                        upload.setConnection(conn);
                        //upload.setConnection((Connection) HibernateUtil.getConnection());
                        //				String connectionString = "jdbc:oracle:thin:@192.168.10.206:1521:db920";
                        //				String user="CFHD";
                        //				String password="CFHD";
                        //				String driver = "oracle.jdbc.driver.OracleDriver";
                        //				Connection con=null;

                        //				try {
                        //					Class.forName(driver);
                        //					con = DriverManager.getConnection(connectionString,user,password); 
                        //				} catch (ClassNotFoundException e3) {
                        //					// TODO Auto-generated catch block
                        //					e3.printStackTrace();
                        //				} catch (SQLException e3) {
                        //					// TODO Auto-generated catch block
                        //					e3.printStackTrace();
                        //				}

                        //				upload.setConnection((Connection) new DBManage().getConnection());
                        //				upload.setConnection(con);				

                        try {
                            upload.upload(userID);
                        } catch (FileNotFoundException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        } catch (ServletException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        } catch (IOException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        } catch (SQLException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                    } catch (FileUploadException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else {
                    if (OLD_FILE.equals("")) {
                        System.out.println("====SEQDETAIL===" + SEQDETAIL);
                        ICaseDocViewer caseDoc = 
                            (CCaseDocManage)CFHDSystem.getControlClass("CASE_DOC");
                        CfhdCaseDoc caseDocEntity = 
                            caseDoc.getCaseDocByPK(new CfhdCaseDocPK(CASEID, 
                                                                     new BigDecimal(SEQDETAIL), 
                                                                     new BigDecimal(0)));
                        if (caseDocEntity != null) {
                            HibernateUtil.getSession().clear();
                            docManage.deleteCaseDoc(new CfhdCaseDoc(new CfhdCaseDocPK(CASEID, 
                                                                                      new BigDecimal(SEQDETAIL), 
                                                                                      new BigDecimal(0))));
                        }
                    }
                } ///end delete file

                System.out.println("++++sendEMail ++++++==" + sendEMail);

                //		if((SELECTACTION.equals(CfhdCaseDetail.ACTION_MANUAL_ESCALATE))&&(!ACTION.equals("save")))
                //		if((SELECTACTION.equals(CfhdCaseDetail.ACTION_TRANSFER))&&(!ACTION.equals("save")))
                if (sendEMail.equals("SUBMIT_ANSWER") || 
                    sendEMail.equals("SUBMIT_REJECT")) {
                    System.out.println("1+++++Begin Send to Mail ++++++" + 
                                       sendEMail);
                    MailManager mm = new MailManager();
                    //	20050518:dho			mm.mailAnswerReject(userID,CASEID);
                    mm.mailAnswerReject(userAcc, CASEID);
                    System.out.println("1+++++End Send to Mail ++++++" + 
                                       sendEMail);
                } else if (sendEMail.equals("SUBMIT_MANUAL_ESCALATE")) {
                    System.out.println("2+++++Begin Send to Mail Escalate++++++");
                    System.out.println("2+++++Begin Send to Mail ++++++CASEID" + 
                                       CASEID);
                    System.out.println("2+++++Begin Send to Mail ++++++userID" + 
                                       userID);
                    MailManager mm = new MailManager();
                    mm.mailEscalateCase(userID, CASEID);
                    System.out.println("2+++++End Send to Mail Escalate++++++");
                } else if (sendEMail.equals("SUBMIT_TRANSFER")) {
                    System.out.println("4.2+++++Begin Send to Mail Transfer++++++");
                    System.out.println("4.2+++++Begin Send to Mail Transfer++++++CASEID" + 
                                       CASEID);
                    System.out.println("4.2+++++Begin Send to Mail Transfer++++++userID" + 
                                       userID);
                    System.out.println("4.2+++++Begin Send to Mail Transfer++++++TRANSFERTO" + 
                                       TRANSFERTO);
                    MailManager mm = new MailManager();
                    mm.mailTransferCase(CASEID, userID, TRANSFERTO);
                    // 20050518:dho			mm.mailTransferCase(CASEID,userAcc,TRANSFERTO);
                    System.out.println("4.2+++++End Send to Mail Transfer++++++");
                }

                System.out.println("======++++++++++++===comment mail=====+++++++++++++++====");
                session.setAttribute("hasError", 
                                     MessageManager.getUpdateSuccessString());
                //successLink=successLink+"&caseID="+CASEID;
                System.out.println("=========successLink successLink=========" + 
                                   successLink);
                //response.sendRedirect(successLink);
                out.println("<SCRIPT type=\"text/javascript\">\n");
                out.print("parent.location='");
                out.print(successLink);
                out.print("';\n");
                out.println("</SCRIPT>");

            } //// end checksize
            else {
                //session.setAttribute("hasError",new CFException(new Exception(MessageManager.getUploadUnsuccessString())));
                session.setAttribute("hasError", 
                                     MessageManager.getUploadUnsuccessString());
                successLink = successLink + "&caseID=" + CASEID;
                response.sendRedirect(successLink);
            }

            //	======================sendRedirect===========> 	
            //	session.setAttribute("hasError",MessageManager.getInsertSuccessString());
            //	response.sendRedirect(successLink);	


        } catch (MailException me) {
            me.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateSuccessString() + 
                                 "But Can not send e-mail");
            System.out.println("=========unsuccessLink Mail unsuccessLink=========" + 
                               unsuccessLink);
            response.sendRedirect(successLink);
        } catch (CFException cfe) {
            cfe.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateUnsuccessString());
            System.out.println("=========unsuccessLink unsuccessLink=========" + 
                               unsuccessLink);
            response.sendRedirect(unsuccessLink + "&submitSuccess=false");
        } catch (HibernateException he) {
            he.printStackTrace();
            session.setAttribute("hasError", 
                                 MessageManager.getUpdateUnsuccessString());
            System.out.println("=========unsuccessLink unsuccessLink=========" + 
                               unsuccessLink);
            response.sendRedirect(unsuccessLink + "&submitSuccess=false");
        }
    }
}// end class CFHDPC07 